// 字符串相加
function add(str1, str2) {
    if (str1.length < str2.length) {
        [str1, str2] = [str2, str1];
    }
    const length1 = str1.length;
    const result = str1.split("");
    let index = 1;
    let flag = 0;
    while (index <= str2.length) {
        const val = str2.substr(-index, 1);
        const cur = length1 - index;
        const newVal = parseInt(result[cur]) + parseInt(val) + flag;
        result[cur] = newVal % 10;
        newVal > 9 ? (flag = 1) : (flag = 0);
        index++;
    }

    while (index <= length1 && flag) {
        const cur = length1 - index;
        const newVal = parseInt(result[cur]) + flag;
        result[cur] = newVal % 10;
        newVal > 9 ? (flag = 1) : (flag = 0);
        index++;
    }

    if (flag) {
        result.unshift(1);
    }
    return result.join("");
}

module.exports = add;